<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>注册商城</title>
    <style>
      * {
        margin: 0;
        padding: 0;
      }
      html,
      body {
        width: 100%;
        height: 100%;
      }
      #box {
        height: 100%;
        width: 100%;
        background: rgb(241, 241, 241);
      }
      #top {
        box-sizing: border-box;
        width: 960px;
        height: 48px;
        margin: auto;
        /* background: pink; */
        padding-top: 13px;
      }
      img {
        display: block;
      }
      .logo {
        width: 110px;
      }
      .left {
        float: left;
      }
      .top-logo {
        background: url(./imgs/fgx.png) no-repeat right;
        /* border-right: 1px solid #888; */
        padding-right: 20px;
      }
      .top-text {
        font-weight: bold;
        margin-left: 20px;
      }
      #button {
        box-sizing: border-box;
        width: 960px;
        height: 100px;
        margin: auto;
        /* background: pink; */
        padding-top: 25px;
      }
      #button p {
        text-align: center;
      }
      #button p span {
        margin: 0 20px;
      }
      #text {
        width: 100%;
        height: 82%;
        margin: auto;
        background: #fff;
        box-sizing: border-box;
      }
      #button p:nth-child(1) {
        color: #999;
        font-size: 14px;
      }
      #button p:nth-child(2) {
        color: rgba(0, 0, 0, 0.3);
        margin-top: 15px;
        font-size: 12px;
      }
      .text-zc {
        padding-top: 80px;
        text-align: center;
        /* background-color: aqua; */
        box-sizing: border-box;
      }
      .text-zc p {
        font-size: 24px;
      }
      a {
        text-decoration: none;
      }
      .text-zc .zc-p {
        font-size: 16px;
        margin-top: 10px;
      }
      .zc-p a {
        color: #007dff;
      }
      .goodsType {
        border: none;
        background: rgba(0, 0, 0, 0.1);
        height: 50px;
        border-radius: 5px;
        background: none;
        outline: none;
        margin: 0 10px;
        font-size: 16px;
      }
      input {
        border: none;
        background: none;
        height: 50px;
        border-radius: 5px;
        font-size: 16px;
        outline: none;
        margin-left: 10px;
      }
      .sjh {
        width: 450px;
        background: rgba(0, 0, 0, 0.1);
        margin: 40px auto;
        border-radius: 5px;
        height: 50px;
        position: relative;
      }
      .left {
        float: left;
      }
      .right {
        float: right;
      }
      .yzm-span {
        color: #007dff;
        font-size: 16px;
        line-height: 50px;
        margin-right: 10px;
      }
      #t2 {
        position: relative;
      }
      .pass {
        position: absolute;
        right: 20px;
        width: 25px;
        height: 25px;
        top: 8px;
      }
      #text .btn {
        background: rgb(235, 166, 170);
        width: 200px;
        height: 40px;
        cursor: not-allowed;
        margin: auto;
        border-radius: 10px;
      }
      .btn p {
        color: #fff;
        font-size: 16px;
        text-align: center;
        line-height: 40px;
      }
      .p1 {
        color: red;
        position: absolute;
        top: -20px;
        left: 0px;
        font-size: 12px !important;
        font-weight: bold;
        display: none;
      }
    </style>
  </head>
  <body>
    <div id="box">
      <div id="top">
        <div class="left top-logo">
          <img src="./imgs/HUAWEI.svg" alt="" class="logo" />
        </div>
        <img src="./imgs/" alt="" />
        <div class="left top-text">
          <p>华为商城</p>
        </div>
      </div>

      <div id="text">
        <div class="text-zc">
          <p>华为账号注册</p>
          <p class="zc-p">已有账号,<a href="login.html">去登录></a></p>
          <div class="sjh">
            <p id="use" class="p1">不是手机号</p>
            <select class="goodsType">
              <option>+86(中国)</option>
              <option>+335(阿尔巴尼亚)</option>
              <option>+93(阿富汗)</option>
              <option>+54(阿根廷)</option></select
            >|
            <input type="text" placeholder="手机号" class="user" />
          </div>

          <div class="sjh">
            <p id="yzm" class="p1">验证码错误</p>
            <input type="text" placeholder="短信验证码" class="yzm left" />
            <span class="right yzm-span">获取验证码</span>
          </div>

          <div class="sjh">
            <div id="t2">
              <p id="pass" class="p1">不是正确格式</p>
              <input
                type="password"
                placeholder="密码 至少八个字符 8-12位"
                class="passi left"
              /><img
                class="pass right"
                src="./imgs/ic_password_eyeoff.svg"
                alt="0"
              />
            </div>
          </div>

          <div class="sjh">
            <div id="t2">
              <p id="passto" class="p1">密码不一致</p>
              <input
                type="password"
                placeholder="确认密码"
                class="passi left passto"
              /><img
                class="pass right"
                src="./imgs/ic_password_eyeoff.svg"
                alt="0"
              />
            </div>
          </div>

          <div class="btn">
            <p>注册</p>
          </div>
        </div>
      </div>

      <div id="button">
        <p>
          华为帐号用户协议 <span>|</span> 关于华为帐号与隐私的声明
          <span>|</span> 常见问题 <span>|</span> Cookies
        </p>
        <p>华为帐号 版权所有 © 2011-2022</p>
      </div>
    </div>
  </body>
</html>
<script>
  let eyeo = document.querySelectorAll(".pass");
  let t2 = document.querySelectorAll("#t2>input");
  eyeo.forEach((res, int) => {
    res.onclick = function () {
      if (this.alt == 0) {
        this.src = "./imgs/ic_password_eyeon.svg";
        this.alt = 1;
        t2[int].type = "text";
        return;
      }
      this.src = "./imgs/ic_password_eyeoff.svg";
      t2[int].type = "password";
      this.alt = 0;
    };
  });

  let user = document.querySelector(".user");
  let pass = document.querySelector(".passi");
  let inp = document.querySelectorAll("input");
  let btn = document.querySelector(".btn");
  let yzm = document.querySelector(".yzm");
  let test_tel =
    /^(13[0-9]|14[01456879]|15[0-35-9]|16[2567]|17[0-8]|18[0-9]|19[0-35-9])\d{8}$/;
  let pass_tel = /^\w{8,12}$/;
  let tr = false;
  yzm.onblur = function () {
    yzm.value.length == 4
      ? (document.getElementById("yzm").style.display = "none")
      : (document.getElementById("yzm").style.display = "block");
  };
  user.onblur = function () {
    if (test_tel.test(user.value)) {
      document.getElementById("use").style.display = "none";
      tr = true;
    } else {
      document.getElementById("use").style.display = "block";
      tr = false;
    }
  };
  pass.onblur = function () {
    if (pass_tel.test(pass.value)) {
      document.getElementById("pass").style.display = "none";
      tr = true;
    } else {
      document.getElementById("pass").style.display = "block";
      tr = false;
    }
  };
  let passto = document.querySelector(".passto");
  passto.onblur = function () {
    if (pass.value == passto.value) {
      document.getElementById("passto").style.display = "none";
      tr = true;
    } else {
      document.getElementById("passto").style.display = "block";
      tr = false;
    }
  };
  inp.forEach((res) => {
    res.oninput = function () {
      // console.log(user.value,pass.value)
      if (
        user.value != "" &&
        pass.value != "" &&
        passto.value != "" &&
        yzm != ""
      ) {
        btn.style.background = "#ca141d";
        btn.style.cursor = "pointer";
      } else {
        btn.style.background = "rgb(235, 166, 170)";
        btn.style.cursor = "not-allowed";
      }
    };
  });

  let yzmSpan = document.querySelector(".yzm-span");
  let num = 0;
  yzmSpan.onclick = function () {
    num = 1;
    if (user.value != "" && use.style.display == "none") {
      let xhr = new XMLHttpRequest();
      xhr.open(
        "get",
        "http://phpclub.org.cn:8080/api/user/sendVerify?" +
          "mobile=" +
          user.value
      );
      console.log("mobile=" + user.value);
      xhr.send();
      xhr.onreadystatechange = function () {
        if (xhr.readyState == 4 && xhr.status == 200) {
          let res = JSON.parse(xhr.responseText);
          console.log(res.data.verifyCode);
          function yzmus() {
            if (yzm.value != res.data.verifyCode || yzm.value == "") {
              document.getElementById("yzm").style.display = "block";
            } else {
              document.getElementById("yzm").style.display = "none";
            }
          }
          yzmus();
          yzm.onblur = function () {
            yzmus();
            // yzmSpan==res.data.verifyCode?console.log("正确"):console.log("错误")
          };
        }
      };
    } else {
      alert("号码为空或号码不正确");
    }
  };

  btn.onclick = function () {
    if (
      use.style.display == "none" &&
      document.getElementById("pass").style.display == "none" &&
      document.getElementById("passto").style.display == "none" &&
      document.getElementById("yzm").style.display == "none" &&
      num
    ) {
        let xhri = new XMLHttpRequest();
        xhri.open("post", "http://phpclub.org.cn:8080/api/user/register");
        xhri.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
        xhri.send(`mobile=${user.value}&verifyCode=${yzm.value}&password=${pass.value}`);
        console.log(user.value,pass.value,yzm.value);
        xhri.onreadystatechange = function () {
          if (xhri.readyState == 4 && xhri.status == 200) {
            console.log(xhri.responseText);
            alert("注册成功")
            location.href="login.html"
          }
        };
    } else {
      num == 0 ? alert("验证码错误") : alert("输入错误或为空");
    }
  };
</script>
